import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        vue(),

        AutoImport({
            resolvers: [ElementPlusResolver()],
        }),

        Components({
            resolvers: [ElementPlusResolver()],
        }),
    ],

    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src',
                import.meta.url))
        }
    },
    server: {
        proxy: {
            '/api': { //检测是否有"/api"开头的请求，并将其转发到后台服务
                target: 'http://localhost:8080', //后台服务所在原
                changeOrigin: true, //是否改变原始主机头
                rewrite: (path) => path.replace(/^\/api/, '') //重写路径，去掉"/api"前缀
            }
        }
    }
})